package net.wangds.auth.menu.conv;

import net.wangds.auth.menu.module.MenuTypeEnum;
import net.wangds.auth.menu.module.SysMenu;
import net.wangds.auth.resource.entity.GeneralResourceEntity;
import net.wangds.log.helper.LogHelper;
import org.springframework.core.convert.converter.Converter;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class Res2SysMenuConv implements Converter<GeneralResourceEntity, SysMenu> {
    @Override
    public SysMenu convert(GeneralResourceEntity source) {

        String sys = source.getSys();
        String module = source.getModule();
        String feature = source.getFeature();
        String activity = source.getActivity();
        String label = source.getResourceLabel();
        String desc = source.getResourceDesc();

        SysMenu res = new SysMenu();
        res.setOid(source.getId());
        res.setId(sys+"_"+module+"_"+feature);
        res.setParentId(sys+"_"+module);

        res.setMenuLabel(label);
        res.setMenuType(MenuTypeEnum.MENU);

        try {
            res.setTargetUrl(URLDecoder.decode(activity, "UTF8"));
        } catch (UnsupportedEncodingException e) {
            LogHelper.error(e);
        }

        res.setIcon(desc);

        return res;
    }
}
